<script setup lang='ts'>
  import { onMounted, reactive, ref, nextTick } from 'vue';
  import { luckDrawApi } from "@/api/controller";
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  const $emit = defineEmits(['updateList'])
  const formLabelWidth = '220px'
  const dataForm = ref()
  const action = reactive({
    disabled:false,
    dialogFormVisible:false,
    title:'',
    formkey:'',
  })
  interface formQuert {
    id:number
    key:string
    integral:number | string
    type:number | string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    integral:[{
      required: true, trigger: 'blur', message: `请输入${action.title}`
    }]
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const { integral } = form
      const num = Number(integral)
      if(!num || !Number(num)) return ToastWarning(`请输入正确的${action.title}`)
      const loading = Toastloading()
      let newForm = {} as any
      newForm[action.formkey] = integral
      luckDrawApi.wishLuckSetStartNum(newForm).then(res=>{
        loading.close()
        $emit('updateList')
        ToastSuccess('修改成功')
        closeDialog()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = (query:any = {})=>{
    let { integral='', text='', key='' } = query
    action.formkey = key
    Object.assign(form,{
      integral,
    })
    action.title = text
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="45%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" :label="action.title" prop="integral">
          <el-input
            v-model="form.integral"
            :placeholder="`请输入${action.title}`"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>